package com.chunhe.sojourn.app;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chunhe.sojourn.app.domain.LinkUrl;
import com.chunhe.sojourn.app.service.LinkUrlService;
import com.chunhe.sojourn.common.controller.BaseController;
import com.chunhe.sojourn.common.domain.QueryRequest;
import com.chunhe.sojourn.common.domain.Result;
import com.chunhe.sojourn.common.exception.FebsException;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @author 8129
 */
@Slf4j
@RestController
@RequestMapping({"api/linkUrl"})
public class LinkUrlController extends BaseController {

    private String message;

    @Resource
    private LinkUrlService linkUrlService;

    @GetMapping
    @ApiOperation("获取链接地址列表（分页）")
    public Map<String, Object> page(QueryRequest request, LinkUrl linkUrl) {
        IPage<LinkUrl> page = this.linkUrlService.getPage(request, linkUrl);
        return getDataTable(page);
    }

    @PostMapping
    @ApiOperation("添加链接地址信息")
    public Result add(LinkUrl linkUrl) throws FebsException {
        try {

            linkUrl.setCreateTime(new Date());
            return Result.OK(this.linkUrlService.save(linkUrl));
        } catch (Exception e) {
            message = "添加链接地址信息失败";
            log.error(message, e);
            return Result.error(message);
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除linkUrl信息")
    public Result delete(@PathVariable Integer id) throws FebsException {
        try {
            return Result.OK(this.linkUrlService.removeById(id));
        } catch (Exception e) {
            message = "删除链接地址信息失败";
            log.error(message, e);
            return Result.error(message);
        }
    }

    @PutMapping
    @ApiOperation("修改linkUrl信息")
    public Result update(LinkUrl linkUrl) throws FebsException {
        try {
            linkUrl.setChannelCode(null);
            linkUrl.setRequestCount(null);
            return Result.OK(this.linkUrlService.updateById(linkUrl));
        } catch (Exception e) {
            message = "修改链接地址信息失败";
            log.error(message, e);
            return Result.error(message);
        }
    }
    @GetMapping("/{id}")
    @ApiOperation("通过ID获取链接地址详情")
    public Result<LinkUrl> detail(@PathVariable Integer id) {
        return Result.OK(this.linkUrlService.getById(id));
    }

}
